﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace GA_Scheduler
{
    public static class PenaltyManager
    {
        private static List<IPenalty> penalties = new List<IPenalty>();


        private static string _path;

        /// <summary>
        /// 
        /// </summary>
        public static string SettingsPath 
        { 
            get
            {
                return _path;
            }
            set
            {
                _path = value;
            }
        }

        public static void Save()
        {
            XDocument xdoc = XDocument.Load(SettingsPath);
            foreach (var mod in penalties)
            {
                xdoc.Root.Elements("Penalty").
                    First(pen => pen.Attribute("name").Value == mod.Name).ReplaceAll(new XAttribute("name", mod.Name), new XAttribute("score", mod.Score));
                
            }

            xdoc.Save(SettingsPath);
                
        }

        public static void Load()
        {
            if (GA_Scheduler.PenaltyManager.Penalties.Count > 0)
            {
                return;
            }

            GA_Scheduler.PenaltyManager.Penalties.Add(new SpacePenalty());
            GA_Scheduler.PenaltyManager.Penalties.Add(new CountDailyPairsPenalty());

           // GA_Scheduler.PenaltyManager.Penalties.Add(new RoomSizePenalty());


            SettingsPath = GA_Scheduler.Properties.Settings.Default.SettingsPath;

            XDocument xdoc = XDocument.Load(SettingsPath);
            var nodes = xdoc.Root.Elements();

            foreach (var node in nodes)
            {
                var penImpl = penalties.FirstOrDefault(pen => pen.Name == node.Attribute("name").Value);
                if (penImpl != null)
                {
                    penImpl.Score = Int32.Parse(node.Attribute("score").Value);
                }
            }
        }

        public static List<IPenalty> Penalties
        {
            get { return penalties; }
        }
    }
}
